package com.sd.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sd.coupon.entity.ItemCoupon;
import com.sd.coupon.model.req.GoodsPageReq;
import com.sd.coupon.model.req.GoodsReq;
import com.sd.coupon.model.res.ItemCouponRes;
import com.sd.coupon.service.IBrowseService;
import com.sd.coupon.service.MBrowseService;
import com.sd.coupon.service.MCollectionService;
import com.sd.coupon.service.MItemCouponService;
import com.sd.coupon.util.rest.RestResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Set;

@Controller
public class ItemController {

    @Resource
    MItemCouponService itemCouponService;

    @Resource
    MCollectionService collectionService;

    @Resource
    MBrowseService browseService;


    @RequestMapping(value = "/itemCouponList", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse getList(@RequestBody GoodsPageReq req) {
        IPage<ItemCoupon> res = itemCouponService.getItemCouponPage(req);
        return RestResponse.success(res);
    }

    @RequestMapping(value = "/itemCoupon", method = RequestMethod.POST)
    @ResponseBody
    public RestResponse get(@RequestBody GoodsReq req) {

        if(req==null ){
            return RestResponse.failure("参数异常");
        }

        //
        if(req.getUserId()!=null ){
            browseService.browseRecord(req);
        }


        ItemCoupon itemCoupon = itemCouponService.getItemCoupon(req);
        if(itemCoupon!=null && itemCoupon.getNumIid()!=null){
            ItemCouponRes res = new ItemCouponRes();
            BeanUtils.copyProperties(itemCoupon,res);

            if(req.getUserId()!=null){
                Set<Long> set = collectionService.getNumIidListByUserId(req.getUserId());
                if (set != null && set.contains(itemCoupon.getNumIid())) {
                    res.setCollected(true);
                }
            }
            return RestResponse.success(res);
        }
        return RestResponse.success(null);
    }


}
